package com.fita.admin.dao;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.fita.entity.agent.Agent;
import com.fita.entity.agent.AgentCompany;
import com.fita.entity.principal.District;
import com.fita.entity.principal.Principal;

import java.util.List;

/**
 *
 * 一级代理
 *
 * @author cuteHU
 *
 */
@Dao("hibernate")
public interface AgentDao {

    /**
     * @param district
     * @return
     */
    @Query("FROM Agent WHERE district = :district AND disabled = false ")
    List<Agent> list(@Variable("district") District district);

    /**
     * @param district
     * @param companies
     * @return
     */
    @Query("FROM Agent WHERE company in (:companies) AND district = :district AND disabled = false ")
    List<Agent> list(@Variable("companies") List<AgentCompany> companies, @Variable("district") District district);
    
    /**
     * @param company
     * @return
     */
    @Query("FROM Agent WHERE disabled = false AND company = :company")
    List<Agent> list(@Variable("company") AgentCompany company);
    
    /**
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM AgentCompany WHERE disabled = false")
    List<AgentCompany> listAgentCompany(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * @return
     */
    @Query("SELECT COUNT(*) FROM AgentCompany WHERE disabled = false AND primaryAgent IS NULL")
    public int countAll();
    
    /**
     * @param district
     * @return
     */
    @Query("SELECT COUNT(*) FROM AgentCompany WHERE district =:district AND disabled = false ")
    public int countOther(@Variable("district") District district);

    /**
     *
     * @param beginIndex
     * @param maxResult
     * @param district
     * @return
     */
    @Limit
    @Query("FROM AgentCompany WHERE district =:district AND disabled = false ")
    public List<AgentCompany> list(@Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult,@Variable("district") District district);

    /**
     * @param name
     * @return
     */
    @Query("FROM AgentCompany WHERE disabled = false AND name = :name")
    public AgentCompany load(@Variable("name") String name);

    @Query("FROM AgentCompany WHERE name =:name AND disabled = false")
    public List<AgentCompany> loads(@Variable("name") String name);

    /**
     * @param mobile
     * @return
     */
    @Query("FROM Agent WHERE mobile =:mobile AND disabled = false ")
    List<Agent> loadAgents(@Variable("mobile") String mobile);
    
    /**
     * 
     * @param mobile
     * @return
     */
    @Query("FROM Agent WHERE mobile =:mobile and disabled=false")
    public Agent loadAgent(@Variable("mobile") String mobile);

    /**
     * @param principal
     * @return
     */
    @Query("FROM AgentCompany WHERE disabled = false AND principal = :principal")
    List<AgentCompany> list(@Variable("principal") Principal principal);

    @Query("FROM Agent WHERE disabled = false AND mobile in (:mobiles)")
    List<Agent> list(@Variable("mobiles") List<String> mobiles);

    /**
     * 根据一级代理公司查找二级代理
     * @param primaryAgentCompany
     * @return
     */
    @Query("FROM Agent WHERE disabled = false AND company.primaryAgent.company = :primaryAgentCompany")
    List<Agent> listSecondaryAgent(@Variable("primaryAgentCompany") AgentCompany primaryAgentCompany);

    @Query("FROM Agent WHERE disabled = false AND mobile = :mobile")
    Agent loadByMobile(@Variable("mobile") String mobile);
}
